import smtplib
from email.header import Header
from email.mime.text import MIMEText

import pandas as pd


def send_mail(to_addrs, message):
    """
    邮件发送
    :param message:
    :return:
    """
    mail_host = 'smtp.qq.com'
    user_name = '642610735@qq.com'
    user_token = 'fmylxsgalsinbbgb'
    from_addr = '642610735@qq.com'

    smtp = smtplib.SMTP()
    smtp.connect(mail_host, 25)
    smtp.login(user_name, user_token)
    smtp.sendmail(from_addr, to_addrs, message.as_string())


def generate_message(data):
    name = data["姓名"]
    in_date = data["入职时间"]
    salary = data["当前月薪"]
    due_days = data["应出勤天数"]
    actual_days = data["实际出勤天数"]
    annual_vacation = data["年假天数"]
    sick_vacation = data["病假天数"]
    thing_vacation = data["事假天数"]
    actual_salary = data["应发税前工资"]

    msg_html_template = """
    <html>
        <body>
            <table>
                <th>
                    <td>姓名</td>
                    <td>入职时间</td>
                    <td>当前月薪</td>
                    <td>应出勤天数</td>
                    <td>实际出勤天数</td>
                    <td>年假天数</td>
                    <td>病假天数</td>
                    <td>事假天数</td>
                    <td>应发税前工资</td>
                </th>
                <tr>
                    <td>{}</td>
                    <td>{}</td>
                    <td>{}</td>
                    <td>{}</td>
                    <td>{}</td>
                    <td>{}</td>
                    <td>{}</td>
                    <td>{}</td>
                    <td>{}</td>
                </tr>
            </table>
        </body>
    </html>
    """
    msg_mail = msg_html_template.format(name, in_date, salary, due_days, actual_days, annual_vacation, sick_vacation,
                                        thing_vacation, actual_salary)

    message = MIMEText(msg_mail, "html", 'utf-8')
    message["from"] = Header("AntTech", "utf-8")
    message["to"] = Header(name, "utf-8")
    message["subject"] = Header(name + "1997年4月份工资条", "utf-8")
    return message


def send_salary():
    """
    发送工资条
    :return:
    """
    data_excel = pd.read_excel("salary.xlsx")
    for index, row in data_excel.iterrows():
        to_addrs = "642610735@qq.com"
        send_mail(to_addrs, generate_message(row))

if __name__ == "__main__":
    send_salary()